<?php
function list_non_karyawan(){

	my_set_code_js('

		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}

	');

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NID'=>array('style'=>'border-bottom:2px solid;width:8%'),   
		'Nama'=>array('style'=>'border-bottom:2px solid;width:37%'),   
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Gol'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'), 
	);

	$query = "SELECT * FROM spd_non_karyawan  a
		INNER JOIN golongan_jabatan b ON a.golongan_jabatan_id = b.golongan_jabatan_id
		 ";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page = 5;  
	$per_page = 28;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 28;  
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['non_karyawan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['non_karyawan_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$golongan_jabatan =my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$ey['golongan_jabatan_id']);
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  strtoupper($ey['nid'] ),   
			'nama'=>  strtoupper($ey['nama'] ),   
			'dir'=>   $ey['jabatan_non_karyawan']   ,   
			'gol'=>  $golongan_jabatan['kode_golongan_jabatan'] ,   
			'operasi'=> position_text_align(	$edit_button .' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  4, false ,$paging);
}	

function new_nid(){
	$query = "SELECT SUBSTR(nid,3,4) AS hasil FROM spd_non_karyawan ORDER  BY non_karyawan_id DESC LIMIT 1 ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$new_id = 1 + (int) $row['hasil'];
	return 'NK'.sprintf( '%04d',$new_id);
}

function form_non_karyawan_create($id =0){
	 
my_set_file_js(
	array(
		'components/system/jquery/combomulti/jquery.chainedSelects.js',
		'components/system/js/calendar/calendarDateInput.js' 
	)
);

	$view = form_header( "non_karyawan" , "non_karyawan"  );
 	$fields = my_get_data_by_id('spd_non_karyawan','non_karyawan_id',$id);
	$newid = new_nid();
	$nid = array(
			'name'=>'nid',
			'value'=>( ($fields )? $fields['nid'] : $newid),
			'id'=>'nid',
			'type'=>'textfield',
			'readonly'=>'yes',
			'style'=>'background-color:#EFEFEF',
			'size'=>'15',
		);
	$form_nid = form_dynamic($nid);
	$view .= form_field_display( $form_nid  , "NID"  );
	
	$nama = array(
			'name'=>'nama',
			'value'=>(isset($_POST['nama'])? $_POST['nama'] : $fields['nama']),
			'id'=>'nama',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($nama);
	$view .= form_field_display( $form_nomor  , "Nama"  );
	$jabatan_non_karyawan = array(
			'name'=>'jabatan_non_karyawan',
			'value'=>(isset($_POST['jabatan_non_karyawan'])? $_POST['jabatan_non_karyawan'] : $fields['jabatan_non_karyawan']),
			'id'=>'jabatan_non_karyawan',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_jabatan_non_karyawan = form_dynamic($jabatan_non_karyawan);
	$view .= form_field_display( $form_jabatan_non_karyawan  , "Jabatan"  );
	  
	$temp_gol = array(
		'name'=>'golongan_jabatan_id',
		'id'=>'golongan_jabatan_id',
		'value'=>(isset($_POST['golongan_jabatan_id']) ? $_POST['golongan_jabatan_id'] : $fields['golongan_jabatan_id'] )
		);	
 
	$query = "SELECT  golongan_jabatan_id,  kode_golongan_jabatan FROM golongan_jabatan  
		ORDER BY kode_golongan_jabatan";	
	$result = my_query($query);
	$opsi_pilihan =array();
	while($row = my_fetch_array($result)){
		 $opsi_pilihan[$row['golongan_jabatan_id']] =  $row['kode_golongan_jabatan']; 
	} 
	$dropdown_gol = form_dropdown($temp_gol , $opsi_pilihan);
	$view .= form_field_display( $dropdown_gol  , "Golongan Jabatan"  );   


	$pengelola = array(
			'name'=>'pengelola',
			'value'=>(isset($_POST['pengelola'])? $_POST['pengelola'] : $fields['pengelola']),
			'id'=>'pengelola',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_pengelola= form_dynamic($pengelola);
	$view .= form_field_display( $form_pengelola  , "Pengelola"  );

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_non_karyawan_validate($id){
	$errsubmit = false;
	$err = array(); 
	 
 	$code =  trim($_POST["nama"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Nama belum di isi";
	}
	$query="SELECT non_karyawan_id FROM spd_non_karyawan 
		WHERE nama='".$code.
		"' AND non_karyawan_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}	

	$code =  trim($_POST["jenis_non_karyawan_id"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Jenis non karyawan belum diisi";
	}
	
/*	
	$code =  trim($_POST["golongan_jabatan_id"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Golongan Jabatan belum diisi";
	}
 */   
 
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function create_type_submit($id = 0){

	if( $id > 0 ){
		$datas = array( 
			'nama'=>my_type_data_str($_POST['nama']),
			'jabatan_non_karyawan'=>my_type_data_str($_POST['jabatan_non_karyawan']),
			'golongan_jabatan_id'=>my_type_data_str( $_POST['golongan_jabatan_id'] ),
			'pengelola'=>my_type_data_str( $_POST['pengelola'] ),
		);
	
	
		return my_update_record('spd_non_karyawan' ,  'non_karyawan_id', $id , $datas);
	}
	$datas = array( 
		'nid'=>my_type_data_str(new_nid()),
		'nama'=>my_type_data_str($_POST['nama']),
		'jabatan_non_karyawan'=>my_type_data_str($_POST['jabatan_non_karyawan']),
		'golongan_jabatan_id'=>my_type_data_str( $_POST['golongan_jabatan_id'] ),
		'pengelola'=>my_type_data_str( $_POST['pengelola'] ),
	);
	return my_insert_record( 'spd_non_karyawan' , $datas);
}

function remove_non_karyawan($id){
	$query= "DELETE FROM spd_non_karyawan WHERE non_karyawan_id = {$id}";
	return my_query($query);
} 